package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.CreateUserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.User;
import com.itheima.service.CreateUserService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CreateUserService.class)
@Transactional
public class CreateUserServiceImpl implements CreateUserService {

    @Autowired
    private CreateUserDao createUserDao;
    @Autowired
    private JedisPool jedisPool;


    @Override
    public void add(User user, String img,Integer[] checkitemIds) {
        //添加用户
        createUserDao.add(user);
        //添加图片
        Integer userId = user.getId();

        //添加关系
        if(checkitemIds != null && checkitemIds.length>0) {
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap();
                map.put("userId",userId);
                map.put("checkitemId",checkitemId);
                createUserDao.addRoleByUid(map);
            }
        }

        Map map = new HashMap();
        map.put("userId",userId);
        map.put("img",img);
        createUserDao.addImg(map);
        //将图片存到缓存
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
    }

    //分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) throws Exception {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);

        Page<User> users = createUserDao.findPage(queryString);

        long total = users.getTotal();
        List<User> result = users.getResult();

        return new PageResult(total,result);
    }

    @Override
    public User findById(Integer id) {

        return createUserDao.findById(id);
    }

    @Override
    public String findByIdByImg(Integer id) {
        return createUserDao.findByImg(id);
    }

    //编辑
    @Override
    public void edit(User user, String img,Integer[] checkitemIds) {

        createUserDao.edit(user);

        //删除现有的关系
        createUserDao.deleteRole(user.getId());

        //重新添加
        if(checkitemIds != null && checkitemIds.length>0) {
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap();
                map.put("userId",user.getId());
                map.put("checkitemId",checkitemId);
                createUserDao.addRoleByUid(map);
            }
        }

        //修改图片
        if(!img.equals("undefined")){
            Map map = new HashMap();
            map.put("uid",user.getId());
            map.put("img",img);
            createUserDao.updateImg(map);
            //将图片存到缓存
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        }
    }

    //删除
    @Override
    public void delete(Integer id) {

        createUserDao.deleteRole(id);
        String img = createUserDao.findByImg(id);
        //七牛云删除图片
        QiniuUtils.deleteFileFromQiniu(img);
        //删除缓存数据
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,img);
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        createUserDao.deleteHeadPortrait(id);

        createUserDao.deleteById(id);
    }
}
